package com.notes.romania;

import java.util.Calendar;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;

import com.notes.romania.broadcastreceiver.AlarmBroadcastReceiver;
import com.notes.romania.reminder.RemManager;
import com.notes.romania.reminder.Reminder;

public class NotificationReceiver extends Activity{
	Reminder rem;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//setContentView(R.layout.notification_result);
		
		//Stop any sound
		AlarmBroadcastReceiver.stopSound();
		
		SharedPreferences settings = getSharedPreferences(RemManager.PREFS_NAME, Context.MODE_PRIVATE);
		SharedPreferences.Editor editor = settings.edit();
 		RemManager.setUp(settings, editor);
 		
 		readIntentExtra(getIntent());
 		
 		if(rem.repeat)
 			createRepeatInterface();
 		else
 			createNormalInterface();
        
	}
	
	 @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
	 
	 private void createRepeatInterface(){
		 final AlertDialog alertDialog = new AlertDialog.Builder(this).create();
	        alertDialog.setTitle(rem.day + "/" + (rem.month + 1) + "/" + rem.year + "  " + rem.hour + " : " + rem.minute);
	        alertDialog.setMessage(rem.text);
	        alertDialog.setIcon(R.drawable.ic_launcher);
	       
	        alertDialog.setButton("Repeat", new DialogInterface.OnClickListener() {
	            public void onClick(DialogInterface dialog, int which) {
	            	NotificationReceiver.this.finish();	//Close the Activity
	            }
	        });
	        alertDialog.setButton2("Dismiss", new DialogInterface.OnClickListener() {
	            public void onClick(DialogInterface dialog, int which) {
	            	RemManager.removeRem(rem);
	            	NotificationReceiver.this.finish();	//Close the Activity
	            }
	        });
	        
	        alertDialog.show();
	        alertDialog.setCancelable(false);
	        alertDialog.setCanceledOnTouchOutside(false);
	 }
	 
	 private void createNormalInterface(){
		 final AlertDialog alertDialog = new AlertDialog.Builder(this).create();
	        alertDialog.setTitle(rem.day + "/" + (rem.month + 1) + "/" + rem.year + "  " + rem.hour + " : " + rem.minute);
	        alertDialog.setMessage(rem.text);
	        alertDialog.setIcon(R.drawable.ic_launcher);
	        alertDialog.setButton("Sleep", new DialogInterface.OnClickListener() {
	            public void onClick(DialogInterface dialog, int which) {    
	            	//Add 5 minutes to date
					
					Calendar c = Calendar.getInstance();
					
					c.add(Calendar.MINUTE, 5);
					
					//Retrieve date
					rem.year = c.get(Calendar.YEAR);
					rem.month = c.get(Calendar.MONTH);
					rem.day = c.get(Calendar.DAY_OF_MONTH);
					rem.hour = c.get(Calendar.HOUR_OF_DAY);
					rem.minute = c.get(Calendar.MINUTE);
					
					//Save the new  reminder
					RemManager.putRem(rem);
					
					//Exit activity
					NotificationReceiver.this.finish();
	            }
	        });
	        alertDialog.setButton2("Dismiss", new DialogInterface.OnClickListener() {
	            public void onClick(DialogInterface dialog, int which) {
	            	NotificationReceiver.this.finish();	//Close the Activity
	            }
	        });
	        alertDialog.show();
	        alertDialog.setCancelable(false);
	        alertDialog.setCanceledOnTouchOutside(false);
	 }
	 
	 private void readIntentExtra(Intent intent){
		 rem = new Reminder();
		 rem.setId(intent.getIntExtra("id", 0));
		 
		 rem.setDateTime(
				 intent.getIntExtra("year", 0),
				 intent.getIntExtra("month", 0),
				 intent.getIntExtra("day", 0),
				 intent.getIntExtra("hour", 0),
				 intent.getIntExtra("minute", 0));
		 
		 rem.setRepeatValues(
				 intent.getIntExtra("yearsDelay", 0),
				 intent.getIntExtra("daysDelay", 0),
				 intent.getIntExtra("hoursDelay", 0),
				 intent.getIntExtra("minutesDelay", 0));
			
		rem.setRepeat(intent.getBooleanExtra("repeat", false));
		
		rem.setText(intent.getStringExtra("text"));
		
		rem.setDefaultSound(intent.getBooleanExtra("defaultSound", true));
		rem.setSoundPath(intent.getStringExtra("soundPath"));
	 }
	 
}
/*
final EditText note = (EditText)findViewById(id.Notification_Note);
note.setText(text);

Button delay = (Button)findViewById(R.id.delayB);
delay.setOnClickListener(new View.OnClickListener() {
	public void onClick(View v) {
		//Add 5 minutes to date
		
		Calendar c = Calendar.getInstance();
		Date d = Calendar.getInstance().getTime();
		
		parseStrings();
		
		c.add(Calendar.YEAR, yearsDelay);
		c.add(Calendar.DAY_OF_YEAR, daysDelay);
		c.add(Calendar.HOUR_OF_DAY, hoursDelay);
		c.add(Calendar.MINUTE, minutesDelay);
		
		//Retrieve date
		year = c.get(Calendar.YEAR);
		month = c.get(Calendar.MONTH);
		day = c.get(Calendar.DAY_OF_MONTH);
		hour = c.get(Calendar.HOUR_OF_DAY);
		minute = c.get(Calendar.MINUTE);
		
		//Save the new  reminder
		editor.putString(prefKey(), note.getText().toString());
		editor.commit();
		
		Toast.makeText(getApplicationContext(), prefKey(), Toast.LENGTH_LONG).show();
		
		//Exit activity
		THIS.finish();
	}
});

Button dismiss = (Button)findViewById(R.id.dismissB);
dismiss.setOnClickListener(new View.OnClickListener() {
	public void onClick(View v) {
		THIS.finish();	//Close the Activity
	}
});
*/
